package ks.app.smartDic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SmartDatabase {

	public static final String KEY_TITLE = "title";
	public static final String KEY_BODY = "body";
	public static final String KEY_ROWID = "_id";

	public static final String KEY_MODE = "mode";
	public static final String KEY_SORT = "sort";
	public static final String KEY_QUERY = "query";
	public static final String KEY_CONTENT = "content";
	public static final String KEY_REG_DATE = "reg_date";
	public static final String KEY_U_DATE = "u_date";
	public static final String KEY_IS_SCRAP = "u_date";
	public static final String KEY_CNT = "cnt";

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "smartDic";
	private static final String DATABASE_TABLE = "smartDic";
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE
			+ " (seq int auto_increment, sort varchar(12), mode int, query varchar(20), content text, cnt int, is_scrape char(1), reg_date datetime, u_date date_time null);";

	private static final int DATABASE_VERSION = 4;

	private final Context context;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			ksLog.d("Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}

	public SmartDatabase(Context ctx) {
		this.context = ctx;
	}

	public SmartDatabase open() throws SQLException {
		mDbHelper = new DatabaseHelper(context);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public SmartDatabase openForRead() throws SQLException {
		mDbHelper = new DatabaseHelper(context);
		mDb = mDbHelper.getReadableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long createWord(String title, String body) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_BODY, body);

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	public long insertWord(String sort, int mode, String query, String content) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_SORT, sort);
		initialValues.put(KEY_MODE, mode);
		initialValues.put(KEY_QUERY, query);
		initialValues.put(KEY_CONTENT, content);
		initialValues.put(KEY_REG_DATE, getDateTime());
		initialValues.put(KEY_IS_SCRAP, "N");

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	public String updateWord(String query, String sort) {

		ContentValues initialValues = new ContentValues();

		initialValues.put(KEY_IS_SCRAP, "Y");

		String[] whereValues = new String[2];
		whereValues[0] = query;
		whereValues[1] = sort;

		mDb.update(DATABASE_TABLE, initialValues, KEY_QUERY + " = '" + query
				+ "' AND " + KEY_SORT + " = '" + sort + "' ", null);

		return "단어 스크랩 완료";
	}

	public boolean deleteNote(long rowId) {

		Log.i("Delete called", "value__" + rowId);
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor fetchAllNotes() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_CONTENT, KEY_CNT },
				null, null, null, null, null);
	}

	public Cursor fetchNote(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
				KEY_BODY }, KEY_ROWID + "=" + rowId, null, null, null, null,
				null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public Cursor selectWord(String sort, int mode, String query) {

		String where = KEY_SORT + " = '" + sort + "' and " + KEY_MODE + " = "
				+ mode + " and query = '" + query + "'";

		ksLog.d("where", where);

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_CONTENT, KEY_CNT },
				where, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		} else {
			return null;
		}

		return mCursor;
	}

	public boolean updateNote(long rowId, String title, String body) {
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, title);
		args.put(KEY_BODY, body);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	private String getDateTime() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}
}